#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


# Program 1.3.8 - Gambler's ruin simulation

Syntax:
python gambler.py 10 20 1000


Notes:
int(sys.argv[1]) == Integer.parseInt(args[0])

sys is a required import here in order to use command line arguments.
https://docs.python.org/2/tutorial/stdlib.html

random is a required import as a substitute for Java's Math.random()
https://docs.python.org/2/library/random.html

What is that underscore doing in the for loop?  The _ can be used as a
placeholder for ignored members in for-loop and tuple assignments.
"for trial in ..." reads better IMO, but Pylint warns on the unused variable.

'''

import sys
import random


def main():
    ''' Probability testing w/Gambler's ruin simulation '''

    stake = int(sys.argv[1])
    goal = int(sys.argv[2])
    trials = int(sys.argv[3])
    bets = 0
    wins = 0
    for _ in range(1, trials + 1):
        cash = stake
        while cash > 0 and cash < goal:
            bets += 1
            if random.random() < 0.5:
                cash += 1
            else:
                cash -= 1
        if cash == goal:
            wins += 1
    print '{}% wins'.format((100 * wins) / trials)
    print 'Avg # bets: {}'.format(bets / trials)


if __name__ == '__main__':
    main()
